home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / CRC_C.ZIP / CRCTAB.C < prev    next >
Text File  |  1987-07-29  |  2KB  |  79 lines

  1. /* generate crc tables for crc-16 and crc-ccitt */
  2. /* crc-16 is based on the polynomial x^16+x^15+x^2+1 */
  3. /*  The bits are inserted from least to most significant */
  4. /* crc-ccitt is based on the polynomial x^16+x^12+x^5+1 */
  5. /*  The bits are inserted from most to least significant */
  6. /* The prescription for determining the mask to use for a given polynomial
  7.     is as follows:
  8.         1.  Represent the polynomial by a 17-bit number
  9.         2.  Assume that the most and least significant bits are 1
  10.         3.  Place the right 16 bits into an integer
  11.         4.  Bit reverse if serial LSB's are sent first
  12. */
  13.  
  14. #include    <stdio.h>
  15. #include    <stdlib.h>
  16. #include    <string.h>
  17.  
  18. #define        M16    0xA001        /* crc-16 mask */
  19. #define        MTT    0x1021        /* crc-ccitt mask */
  20.  
  21. /* function declarations */
  22. unsigned int updcrc(unsigned int,int,unsigned int);
  23. unsigned int updcrcr(unsigned int,int,unsigned int);
  24. void perr(char *);
  25.  
  26. /* driver */
  27. main()
  28. {
  29.     int i,j;
  30.     printf("\n\t");
  31.     for(i=0;i<32;i++)
  32.     {
  33.         for(j=0;j<8;j++) printf("0x%04X, ",updcrcr(0,8*i+j,M16));
  34.         printf("\n\t");
  35.     }
  36.     printf("\n\t");
  37.     for(i=0;i<32;i++)
  38.     {
  39.         for(j=0;j<8;j++) printf("0x%04X, ",updcrc(0,8*i+j,MTT));
  40.         printf("\n\t");
  41.     }
  42. }
  43.  
  44. /* update crc */
  45. unsigned int updcrc(crc,c,mask)
  46. unsigned int crc,mask; int c;
  47. {
  48.     int i;
  49.     c<<=8;
  50.     for(i=0;i<8;i++)
  51.     {
  52.         if((crc ^ c) & 0x8000) crc=(crc<<1)^mask;
  53.         else crc<<=1;
  54.         c<<=1;
  55.     }
  56.     return crc;
  57. }
  58.  
  59. /* update crc reverse */
  60. unsigned int updcrcr(crc,c,mask)
  61. unsigned int crc,mask; int c;
  62. {
  63.     int i;
  64.     for(i=0;i<8;i++)
  65.     {
  66.         if((crc ^ c) & 1) crc=(crc>>1)^mask;
  67.         else crc>>=1;
  68.         c>>=1;
  69.     }
  70.     return crc;
  71. }
  72.  
  73. /* error abort */
  74. void perr(s)
  75. char *s;
  76. {
  77.     printf("\n%s",s); exit(1);
  78. }
  79.